﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MySilverlightAlbum.Data;

namespace MySilverlightAlbum
{
    public partial class ImageList: UserControl
    {
        private const int                               POPUP_MARGIN = 20;
        public event RoutedEventHandler                 ImageSelected;
        private ImageFolder                             m_current;
        private double                                  m_yOffset = 0;
        private StackPanel                              m_panel;
        private ScrollViewer                            m_scroll;

        public ImageList()
        {
            InitializeComponent();
        }

        void OnScrollMouseWheel(
            object                                      sender, 
            MouseEventArgs                              e
        )
        {
            Point                                       pos;

            pos = e.GetPosition(this);
            
            m_yOffset = pos.Y / this.ActualHeight;

            ScrollToOffset();
        }

        private void ScrollToOffset(
        )
        {
            if (m_panel != null && m_scroll != null)
            {
                m_scroll.ScrollToVerticalOffset(m_yOffset * 
                    (m_panel.ActualHeight - this.ActualHeight));
            }
        }

        public ImageFolder CurrentFolder
        {
            get
            {
                return m_current;
            }

            set
            {
                m_current = value;
                imageList.ItemsSource = value.Children;
            }
        }

        private void OnImageSelected(
            object                                      sender, 
            MouseButtonEventArgs                        e
        )
        {
            if (ImageSelected != null)
            {
                ImageSelected(((FrameworkElement) sender).DataContext, e);
            }
        }

        private void OnStackPanelLoaded(
            object                                      sender, 
            RoutedEventArgs                             e
        )
        {
            m_panel = sender as StackPanel;
        }

        private void OnScrollViewerLoaded(
            object                                      sender, 
            RoutedEventArgs                             e
        )
        {
            m_scroll = sender as ScrollViewer;
        }

        private void Image_MouseEnter(
            object                                      sender, 
            MouseEventArgs                              e
        )
        {
            Point                                       p;
            double                                      screenHeight;

            if (!imagePopup.IsOpen)
            {
                p = e.GetPosition(m_scroll);
                screenHeight = App.Current.Host.Content.ActualHeight;

                if (p.Y + popupBorder.Height > screenHeight - POPUP_MARGIN)
                {
                    imagePopup.VerticalOffset =  screenHeight - popupBorder.Height - POPUP_MARGIN;
                }
                else
                {
                    imagePopup.VerticalOffset = p.Y; 
                }
                imagePopup.DataContext = (sender as Image).DataContext;
                
                
                imagePopup.IsOpen = true;
            }

        }

        private void Image_MouseLeave(
            object                                      sender, 
            MouseEventArgs                              e
        )
        {
            imagePopup.IsOpen = false;
        }
    }
}
